﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Collections;

namespace FeatureModelCore.FMAnalyzer
{
    public class FMProduct
    {
        #region Properties
        public FMProductItem[] Items { get { return _items.ToArray<FMProductItem>(); } }
        public int ItemsCount { get { return _items.Count; } }
        #endregion

        #region Fields
        /// <summary>
        /// Items List
        /// </summary>
        private FMSet<FMProductItem> _items; 
        #endregion

        /// <summary>
        /// 
        /// </summary>
        internal FMProduct()
        {
            _items = new FMSet<FMProductItem>();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="itemNames"></param>
        internal FMProduct(string[] itemNames)
        {
            _items = new FMSet<FMProductItem>();
            foreach (string str in itemNames)
            {
                _items.Add(new FMProductItem(str));
            }
        }
        /// <summary>
        /// Add items
        /// </summary>
        /// <param name="name"></param>
        internal void AddItem(string name)
        {
            this._items.Add(new FMProductItem(name));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (_items.Count == 0)
            {
                return string.Empty;
            }
            StringBuilder strBuilder = new StringBuilder(_items[0].ToString());
            for (int i = 1; i<_items.Count; ++i)
            {
                strBuilder.AppendFormat(",{0}", _items[i]);
            }
            return "{" + strBuilder.ToString() + "}";
        }

        #region Equals
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            FMProduct tmpProduct = obj as FMProduct;
            if (tmpProduct == null)
            {
                return false;
            }
            FMSet<FMProductItem> curItems = new FMSet<FMProductItem>(this._items);
            FMSet<FMProductItem> tmpItems = new FMSet<FMProductItem>(tmpProduct._items);
            return this._items.Count == tmpProduct._items.Count
                && curItems.Equals(tmpItems);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion
    }
}
